package com.gxy.learn.backup.web;//package com.gxy.learn.web;
//
//import com.google.common.util.concurrent.ThreadFactoryBuilder;
//import com.gxy.learn.task.BackupDataTask;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.web.bind.annotation.GetMapping;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RestController;
//
//import java.util.concurrent.*;
//
//@Slf4j
//@RequestMapping("/task")
//@RestController
//public class ConsumptionTaskController {
//
//    @Autowired
//    private BackupDataTask task;
//
//    @GetMapping("test")
//    public void test() {
//        ThreadFactory consumptionThreadFactory = new ThreadFactoryBuilder().setNameFormat("consumption-pool-%d").build();
//
//        ExecutorService consumptionExecutor = new ThreadPoolExecutor(10, 20,
//                0L, TimeUnit.MILLISECONDS,
//                new LinkedBlockingQueue<Runnable>(10), consumptionThreadFactory, new RejectedExecutionHandler() {
//            @Override
//            public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
//                if (!executor.isShutdown()) {
//                    try {
//                        executor.getQueue().put(r);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                }
//            }
//        });
//        ThreadPoolExecutor executor = (ThreadPoolExecutor) consumptionExecutor;
//        for (int i = 0; i < 100; i++) {
//            int finalI = i;
//            log.info("i = [{}] ,consumptionExecutor.isShutdown() = {}", i, consumptionExecutor.isShutdown());
//            log.info("i = [{}] ,executor.getQueue().size() = {}", i, executor.getQueue().size());
//            if (consumptionExecutor.isShutdown()) {
//                break;
//            }
//            consumptionExecutor.submit(new Runnable() {
//                @Override
//                public void run() {
//
//                    if (finalI == 10) {
//                        log.info("i = [{}] ,Thread.currentThread().getState()) = {},开始关闭线程池", finalI, Thread.currentThread().getState());
//                        consumptionExecutor.shutdown();
//                    } else {
//                        log.info("i = [{}] ,Thread.currentThread().getState()) = {}", finalI, Thread.currentThread().getState());
//                    }
//                    try {
//                        TimeUnit.SECONDS.sleep(15);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                }
//            });
//        }
//        while (true){
//            log.info("consumptionExecutor.isTerminated() = {},executor.getQueue().size() = {}", consumptionExecutor.isTerminated(),executor.getQueue().size());
//            try {
//                TimeUnit.SECONDS.sleep(2);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        }
//    }
//}
